<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <meta charset='UTF-8' />
    <title>Output of rails_best_practices</title>
    <style type="text/css">
      body {
        color: #333;
        background: #eee;
        padding: 0 20px;
      }
      h1 {
        color: ##4E4E4E;
      }
      p {
        margin: 5px 0;
      }
      table {
        background: white;
        border: 1px solid #666;
        border-collapse: collapse;
        margin: 10px 0;
        font-size: 14px;
      }
      table.result th, table.result td {
        padding: 4px;
        border: 1px solid #D0D0D0;
      }
      table.result th {
        background-color: #DFC;
        color: #337022;
      }
      table.result td.filename {
        color: #ED1556;
      }
      table.result tr:hover {
        background-color: #FFFFC0;
      }
      ul {
        clear: both;
        display: inline-block;
        padding: 0;
        margin: 0;
      }
      ul li {
       list-style: none;
       display: none;
       float: left;
      }
      .result tbody tr{
        display: none;
      }
    </style>
<%
  def columnize(arr, col_count)
    row_count = arr.size / col_count
    row_count += 1 if arr.size % col_count > 0
    cols = arr.each_slice(row_count).to_a
    cols[0].zip(*cols[1..-1]).map(&:compact)
  end
%>
  </head>
  <body>
    <h1>rails_best_practices output</h1>
    <p>
      Please go to
      <a href='http://rails-bestpractices.com' target='_blank'>http://rails-bestpractices.com</a>
      to see more useful Rails Best Practices.
    </p>
    <p>
      <% if @errors.empty? %>
        No error found. Cool!
      <% else %>
        Found <%= @errors.size %> warnings.
      <% end %>
    </p>
    <table>
      <% unless @errors.empty? %>
        <% columnize(@error_types, 3).each do |row| %>
          <tr>
            <% row.map { |error_type| error_type.split(':').last }.each do |error_type| %>
              <td>
                <input type="checkbox" class="error-type" id="<%= error_type %>" value="<%= error_type %>"
                />&nbsp;<label for="<%= error_type %>"><%= error_type.sub(/(Check|Review)$/, '').gsub(/([a-z\d])([A-Z])/,'\1 \2') %></label>
              </td>
            <% end %>
          </tr>
        <% end %>
        <tr>
          <td colspan="3">
            <button id="show-all">Check all</button>
            <button id="show-none">Uncheck all</button>
          </td>
        </tr>
      <% end %>
    </table>
    <table class="result">
      <thead>
        <tr>
          <th>Filename</th>
          <th>Line Number</th>
          <th>Warning Message</th>
          <% if @hg %>
            <th>Hg Commit</th>
            <th>Hg Username</th>
          <% elsif @git %>
            <th>Git Commit</th>
            <th>Git Username</th>
          <% end %>
        </tr>
      </thead>
      <tbody>
        <% @errors.each do |error| %>
          <tr class="<%= error.type.split(':').last %>">
            <td class='filename'>
              <% if @github %>
                <a href='<%= @github_name %>/blob/<%= @last_commit_id %>/<%= error.short_filename %>#L<%= error.first_line_number %>' target='_blank'><%= error.short_filename %></a>
              <% elsif @textmate %>
                <a href='txmt://open/?url=file://<%= File.expand_path(error.filename) %>&amp;line=<%= error.line_number %>'><%= error.short_filename %></a>
              <% elsif @sublime %>
                <a href='subl://open?url=file://<%= File.expand_path(error.filename) %>&amp;line=<%= error.line_number %>'><%= error.short_filename %></a>
              <% elsif @mvim %>
                <a href='mvim://open/?url=file://<%= File.expand_path(error.filename) %>&amp;line=<%= error.line_number %>'><%= error.short_filename %></a>
              <% else %>
                <%= error.short_filename %>
              <% end %>
            </td>
            <td class='line'><%= error.line_number %></td>
            <td class='message'>
              <a href='<%= error.url %>' target='_blank'><%= error.message %></a>
            </td>
            <% if @hg %>
              <td class='hg_commit'><%= error.hg_commit %></td>
              <td class='hg_usename'><%= error.hg_username %></td>
            <% elsif @git %>
              <td class='git_commit'><%= error.git_commit %></td>
              <td class='git_usename'><%= error.git_username %></td>
            <% end %>
          </tr>
        <% end %>
      </tbody>
    </table>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
      $(function() {

        keys = location.hash.slice(2).split("|");
        keys = keys.filter(function(item){
          return item != "";
        });
        keys.forEach(function(item){
          $('.'+item).show();
          $('input.error-type#'+item+'[type=checkbox]').prop('checked', true);
        });

        $('input.error-type[type=checkbox]').click(function() {
          if ($(this).attr('checked')) {
            $(this).prop('checked', true);
            $('.'+$(this).val()).show();

            new_hash = location.hash.replace("|"+$(this).val().toString(), "");
            location.hash = new_hash+"|"+$(this).val();
          } else {
            $(this).prop('checked', false);
            $('.'+$(this).val()).hide();

            new_hash = location.hash.replace("|"+$(this).val().toString(), "");
            location.hash = new_hash
          }
        });

        $('#show-all').click(function() {
          $('input.error-type[type=checkbox]').each(function(index, item){
            new_hash = location.hash.replace("|"+$(item).val().toString(), "");
            location.hash = new_hash+"|"+$(item).val();
          });
          $('input.error-type[type=checkbox]').prop('checked', true);
          $('table.result tbody tr').show();
        });

        $('#show-none').click(function() {
          $('input.error-type[type=checkbox]').prop('checked', false);
          $('table.result tbody tr').hide();
          location.hash = "";
        });
      });
    </script>
  </body>
</html>
